//
//  EPAlertViewController.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/12/5.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "EPAlertViewController.h"

@interface EPAlertViewController ()

/// 模型视图
@property (nonatomic, readonly, strong) EPAlertViewModel* viewModel;

// UI
@property (nonatomic, weak) IBOutlet UILabel* titleLabel;
@property (nonatomic, weak) IBOutlet UILabel* contentLabel;
@property (nonatomic, weak) IBOutlet UIButton* cancelBtn;
@property (nonatomic, weak) IBOutlet UIButton* bgBtn;
@property (nonatomic, weak) IBOutlet UIButton* confirmButton;

@end

@implementation EPAlertViewController
@dynamic viewModel;

- (instancetype)initWithViewModel:(SUViewModel2 *)viewModel
{
    self = [super initWithViewModel:viewModel];
    if (self)
    {
        [self initData];
        [self initLoadForRAC];
        [self initUI];
    }
    return self;
}

- (void)initData
{
    
}

- (void)initLoadForRAC
{
    @weakify(self)
    [[self rac_signalForSelector:@selector(viewDidLoad)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
}

- (void)initUI
{
}

- (void)bindViewModel
{
    [super bindViewModel];
    
    @weakify(self)
    
    RACSignal* willAppearSignal = [[self rac_signalForSelector:@selector(viewWillAppear:)] replay];
    RACSignal* didAppearSignal = [[self rac_signalForSelector:@selector(viewDidAppear:)] replay];
    RACSignal* viewDidLayoutSVSignal = [[self rac_signalForSelector:@selector(viewDidLayoutSubviews)] replay];
    
    [willAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        self.view.backgroundColor = [UIColor clearColor];
        
        self.titleLabel.text = self.viewModel.strTitle;
        self.contentLabel.text = self.viewModel.strContent;
        
        if (self.viewModel.strConfirm && [self.viewModel.strConfirm isNotBlank])
        {
            [self.confirmButton setTitle:self.viewModel.strConfirm forState:UIControlStateNormal];
        }
    }];
    
    [didAppearSignal subscribeNext:^(id x) {
        @strongify(self)
        
        NSLog(@"viewDidAppear");
        
        // ScrollView 内容 未能在此时才约束
        
        //Request
        
    }];
    
    [[self rac_signalForSelector:@selector(viewWillDisappear:)] subscribeNext:^(id x)
     {
         @strongify(self)
         
     }];
    
    [[[[viewDidLayoutSVSignal take:1] zipWith:didAppearSignal] deliverOnMainThread] subscribeNext:^(id x)
     {
         @strongify(self)
         
         //重置 更新 frame
         [self _setupAllView];
     }];
    
#pragma mark -- bindViewModel
    
    UserManagerV2* um = [UserManagerV2 sharedUserManagerV2];
    
}

- (void)_setupAllView
{
    @weakify(self);
    
    [self.bgBtn addTarget:self action:@selector(_btnListener_bg) forControlEvents:UIControlEventTouchUpInside];
    [self.cancelBtn addTarget:self action:@selector(_btnListener_bg) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmButton addTarget:self action:@selector(_btnListener_confirm) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - Listener
- (void)_btnListener_bg
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)_btnListener_confirm
{
    @weakify(self);
    
    [self.view resignFirstResponder];
    
    [self dismissViewControllerAnimated:YES completion:^{
        @strongify(self);
        
        [self.viewModel.confirmCmd execute:nil];
    }];
}

#pragma mark - Delegate
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
